package com.yxq.mdt.app.modules.personal.ui.activity;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseActivity;
import com.yxq.mdt.app.modules.personal.adapter.PersonalMyFootPrintListAdapter;
import com.yxq.mdt.app.modules.personal.bean.FootBean;
import com.yxq.mdt.app.modules.personal.bean.RecordUserListBean;
import com.yxq.mdt.app.modules.personal.model.FootModel;
import com.yxq.mdt.app.modules.personal.presenter.FootPresenter;
import com.yxq.mdt.app.modules.personal.presenter.IFootContract;

import butterknife.BindView;

/**
 * 我的足迹
 */
public class MainPersonalMyFootprintAc extends BaseActivity<FootPresenter> implements IFootContract.View {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    PersonalMyFootPrintListAdapter personalMyFootPrintListAdapter;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main_personal_my_footprint;
    }

    @Override
    public void initView() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                mPresenter.selectMyAccessRecord(1, 20);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                mPresenter.selectMyAccessRecord(page, 20);
            }
        });
        personalMyFootPrintListAdapter = new PersonalMyFootPrintListAdapter(R.layout.item_foot_print_list_layout, null);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(personalMyFootPrintListAdapter);
        personalMyFootPrintListAdapter.setEmptyView(R.layout.layout_empty_view, recyclerView);
        personalMyFootPrintListAdapter.isUseEmpty(false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mPresenter = new FootPresenter(this, new FootModel());
        refreshLayout.autoRefresh();
    }

    @Override
    public void onLoadSuccess(FootBean footBean) {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
        if (page == 1) {
            personalMyFootPrintListAdapter.setNewData(footBean.getList());
        } else {
            if (footBean.getList() != null && footBean.getList().size() != 0) {
                personalMyFootPrintListAdapter.addData(footBean.getList());
            } else {
                refreshLayout.finishLoadMoreWithNoMoreData();
            }
        }
        personalMyFootPrintListAdapter.isUseEmpty(!(personalMyFootPrintListAdapter.getData().size() > 0));
    }

    @Override
    public void onLoadFailed(String msg) {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
    }

    @Override
    public void onLoadUserListSuccess(RecordUserListBean recordUserListBean) {

    }

    @Override
    public void onLoadUserListFailed(String msg) {

    }
}
